<?php

namespace app\models;

use app\models\Commic;
use yii\db\ActiveRecord;
use Yii;

class Commic extends ActiveRecord{
	
	
	public static function tableName(){
		// return "shop_admin";
		return "{{%Commic}}";
	}
	
	//uId 用户序号	uName 用户昵称	uMail 用户邮箱	uNumber 用户账号	uPassword 密码	uHeadurl 头像 state

	public function rules(){
        return [
            [['cId'], 'required'],
        ];
    }

    public function attributeLabels(){
        return [
            'cId' => '漫画id',
            'cTid' => '漫画类型id',
            'cName' => '漫画名称',
            'cAuthor' => '漫画作者',
            'cCover' => '漫画封面',
            'cCollection' => '漫画收藏量',
            'cReadcount' => '漫画阅读量',
            'cNowupdate' => '漫画最新一集',
            'cSaveurl' => '储存路径',
            'cUpdatetime' => '最新更新时间',
            'state' => '状态',
        ];
    }
    
    //插入数据
    public function insertCommic($data){
        //load是php框架内部自主加载数据的函数，而validate是自主验证rules
    }

    //查找所有漫画
    public function getAll(){
    	$data = self::find()->select(['cId','cTid','cName','cAuthor','cCover'])->where('state = :state',[':state'=>'1'])->asArray()->all();
		return $data;
    }

    //查找对应名字的漫画
    public function getSearch($expression){
        if(!empty($expression)){
            //User::findBySql('SELECT * FROM user')->all();  此方法是用 sql  语句查询 user 表里面的所有数据； 
            $sql = 'SELECT cId,cTid,cName,cAuthor,cCover FROM Commic WHERE cName REGEXP '.'"'.$expression.'"';
            $result = Commic::findBySql($sql)->all();
            if(!empty($result)){
                return $result;
            }  
        }
        return false;
    }

    //根据cid获取某一漫画全部信息
    public function getBycId($cId){
        $data = self::find()->select(['cId','cName','cAuthor','cCover','cCollection','cReadcount','cSaveurl','cNowupdate','cUpdatetime'])->where('cId = :cId and state = :state',[':cId'=>$cId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    //根据cid获取某一漫画的名字和封面
    public function getRecordInfoBycId($cId){
        $data = self::find()->select(['cId','cName','cCover','cSaveurl'])->where('cId = :cId and state = :state',[':cId'=>$cId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    //根据cid获取某一漫画的名字和封面和储存路径
    public function getInfoBycId($cId){
        $data = self::find()->select(['cId','cName','cCover','cNowupdate'])->where('cId = :cId and state = :state',[':cId'=>$cId,':state'=>'1'])->asArray()->one();
        return $data;
    }

    //查找所有类型名称
    public function getBytype($cTid){
    	$data = self::find()->select(['cId','cName','cAuthor','cCover'])->where('cTid = :cTid and state = :state',[':cTid'=>$cTid,':state'=>'1'])->orderBy('cUpdatetime desc')->asArray()->all();
		return $data;
    }

    //获取收藏量排行榜
    public function getBycollection(){
    	$data = self::find()->select(['cId','cName','cCover','cCollection','cNowupdate','cUpdatetime'])->where('cCollection > :cCollection and state = :state',[':cCollection'=>'0',':state'=>'1'])->orderBy('cCollection desc')->asArray()->all();
		return $data;
    }

    //获取阅读量排行榜
    public function getByreadcount(){
    	$data = self::find()->select(['cId','cName','cCover','cReadcount','cNowupdate','cUpdatetime'])->where('cReadcount > :cReadcount and state = :state',['cReadcount'=>'0',':state'=>'1'])->orderBy('cReadcount desc')->asArray()->all();
		return $data;
    }

    //获取更新时间最新的漫画top10
    public function getByUpdatetopten(){
        $data = self::find()->select(['cId','cName','cCover','cNowupdate', 'cUpdatetime'])->where('state = :state',[':state'=>'1'])->orderBy('cUpdatetime desc')->asArray()->all();
        return $data;
    }

    //获取更新时间最新的漫画
    public function getByUpdatetopthree(){
        $data = self::find()->select(['cId','cName','cCover'])->where('state = :state',[':state'=>'1'])->orderBy('cUpdatetime desc')->limit(3)->asArray()->all();
        return $data;
    }

    //更新某漫画收藏量
    public function updateCollection($cId){
        $commic_info = Commic::getBycId($cId);
        $cCollection = $commic_info['cCollection'];
        $key = [
            'cId' => $cId,
        ];
        $changedata = [
            'cCollection' => $cCollection+1,
        ];
        $result = Commic::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }

    //取消收藏的收藏量
    public function updateUnCollection($cId){
        $commic_info = Commic::getBycId($cId);
        $cCollection = $commic_info['cCollection'];
        $key = [
            'cId' => $cId,
        ];
        $changedata = [
            'cCollection' => $cCollection-1,
        ];
        $result = Commic::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }

    //阅读量更新
    public function updateReadcount($cId){
        $commic_info = Commic::getBycId($cId);
        $cReadcount = $commic_info['cReadcount'];
        $key = [
            'cId' => $cId,
        ];
        $changedata = [
            'cReadcount' => $cReadcount+1,
        ];
        $result = Commic::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }
}
